package yxy.game.pm2.active.module.actcontrol;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.redisson.api.RBucket;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actcontrol;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.bean.module.BaseGameRedisBean;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.bean.timeline.TimelineModule;

import java.util.Map;

/**
 * 活动控制  timeline控制 gm修改活动开关为修改timeline的start和end
 * @author Dell
 *
 */
public class activeControl extends TimelineModule {

	private String areaid;	//区id
	private String acttype; // 活动id
	private String name;
	private DateTime starttime; // 活动开启时间
	private DateTime endtime; // 活动结束时间
	private int opentype; // 0.按照开始时间 1.按照开服时间
	private int circledays; // 循环间隔天数
	private int operationcycle;	//运行时间
	private int daycondition; //开服天数开启条件

	public String getAreaid() {
		return areaid;
	}
	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}
	public String getActtype() {
		return acttype;
	}
	public void setActtype(String acttype) {
		this.acttype = acttype;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public DateTime getStarttime() {
		return starttime;
	}
	public void setStarttime(DateTime starttime) {
		this.starttime = starttime;
	}
	public DateTime getEndtime() {
		return endtime;
	}
	public void setEndtime(DateTime endtime) {
		this.endtime = endtime;
	}
	public int getOpentype() {
		return opentype;
	}
	public void setOpentype(int opentype) {
		this.opentype = opentype;
	}
	public int getCircledays() {
		return circledays;
	}
	public void setCircledays(int circledays) {
		this.circledays = circledays;
	}
	public int getOperationcycle() {
		return operationcycle;
	}
	public void setOperationcycle(int operationcycle) {
		this.operationcycle = operationcycle;
	}
	public int getDaycondition() {
		return daycondition;
	}
	public void setDaycondition(int daycondition) {
		this.daycondition = daycondition;
	}
	// info===========================================================================

	public static final int 已有数据 = 1;
	public static final int 添加成功 = 2;
	public static final int 添加失败 = 3;
	public static final int 无初始数据 = 4;

	// 某个服某个活动是否开启
	public boolean isRuning() {
		return running();
	}

	/**
	 * 获取某个服某个活动信息
	 *
	 * @param areaid
	 * @param acttype
	 * @return
	 */
	public static activeControl getAreaOneActionInfo(String areaid, String acttype) {
		activeControl sai = get(areaid, acttype);
		return sai;
	}

	public static activeControl getAreaOneActionInfo(int areaid, String acttype) {
		activeControl sai = get(String.valueOf(areaid), acttype);
		return sai;
	}

	/**
	 * 创建区活动信息
	 * @param areaid
	 * @param data
	 * @return
	 */
	public static activeControl create(int areaid, actcontrol data) {
		activeControl sai = new activeControl();

		int acttype = data.getId();

		DateTimeFormatter datetimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd#HH:mm:ss");
		DateTime start = DateTime.parse(data.getBegintime(), datetimeFormat);
		DateTime end = DateTime.parse(data.getEndtime(), datetimeFormat);

		sai.setAreaid(String.valueOf(areaid));
		sai.setActtype(String.valueOf(acttype));
		sai.setName(data.getName());
		sai.setCircledays(data.getCycle());
		sai.setEndtime(end);
		sai.setOpentype(data.getOpentype());
		sai.setStarttime(start);
		sai.setDaycondition(data.getDaycondition());
		sai.setOperationcycle(data.getOperationcycle());//活动运行天数
		int cycle = data.getCycle();
		long circletime = 0;
		if (cycle == -1) {
			circletime = end.getMillis() - start.getMillis();
		} else {
			circletime = cycle * ActiveUtil.DAY;
		}

		Timeline tl = new Timeline();
		tl.setStartTime(start);
		tl.setEndTime(end);
		tl.setCircleTime(circletime);
		tl.setRound(0);

		sai.setTimeline(tl);
		sai.set();
		return sai;
	}

	/**
	 * 获取某个服所有活动信息
	 *
	 * @param sid
	 */
	public JSONObject getAreaActionInfo(String areaid, String playercode) {
		JSONObject actsinfo = new JSONObject();

		Iterable<String> keys = redis2().client().getKeys().getKeysByPattern(key(areaid, "*"));
		for (String key : keys) {
			String[] ks = key.split(":");
			String actid = ks[ks.length - 1];
			activeControl sai = get(key);
			if (sai.isRuning()) {
				actsinfo.put(actid, true);
			}
		}
		return actsinfo;
	}

	/**
	 * 初始化区所有活动控制
	 * @param areaid 区
	 * @return
	 */
	public static int initactivecontrol(int areaid) {
		try {
			Map<String, actcontrol> acmap = DataCenter.getAllData(actcontrol.class);
			if(acmap == null || acmap.isEmpty()) {
				return 无初始数据;
			}

			int i = 0;
			for(actcontrol data : acmap.values()) {
				activeControl control = activeControl.get(areaid, data.getType());
				if(control == null) {
					activeControl.create(areaid, data);
				}else {
					i++;
				}
			}

			if(i == acmap.size()) {
				return 已有数据;
			}

			return 添加成功;
		} catch (Exception e) {
			return 添加失败;
		}
	}


	// redis==========================================================================

	private static String key(String areaid, String acttype) {
		return String.format("AREA:%s:ACTTYPE:%s", areaid, acttype);
	}

	public void set() {
		try {
			setv2(key(getAreaid(), getActtype()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	public void set(String key) {
		try {
			setv2(key, this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public activeControl get(String areaid, String acttype) {
		try {
			activeControl module = getv2(key(areaid, acttype));
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	static public activeControl get(int areaid, int acttype) {
		return get(String.valueOf(areaid), String.valueOf(acttype));
	}

	static public activeControl get(String key) {
		try {
			activeControl module = getv(key);
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}


	static protected RedisConnection redis2() {
		return RedisManager.getInstance().getConnection(RedisType.REDIS_DATA);
	}

	static protected <V> RBucket<V> touchBucket2(String key) throws RedisException {
		return RedisManager.client(redis2()).getBucket(key);
	}

	protected static <V extends BaseGameRedisBean> V getv2(String key) throws RedisException {
		RBucket<V> bucket = touchBucket2(key);
		return bucket.get();
	}

	protected static <V extends BaseGameRedisBean> void setv2(String key, V m) throws RedisException {
		RBucket<V> bucket = touchBucket2(key);
		bucket.set(m);
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getAreaid(), getActtype()));
	}

}
